App.module.UserRole.Role.Permission = Ext.extend(Ext.util.Observable, {
	init: function(tree){
		this.tree = tree;
		
		tree.on('render', function(tree){
			var bt = new Ext.Button({
				tooltip: 'Permission'
				,iconCls: 'icon-update-16x16'
				,disabled: true
				,handler: this.showWin
				,scope: this
			});
			tree.getTopToolbar().add('->', bt);
			tree.getSelectionModel().on('selectionchange', function(sm, node){
				bt.setDisabled(!node);	
			});
		}, this);	
	}
	,load: function(){
		var node = this.tree.getSelectionModel().getSelectedNode();
		if(!node) return;
		
		API.role.loadPermission({id: node.id}, function(r){
				
		});	
	}
	,getForm: function(){
		if(this.form) return this.form;
		
		var o = this;
		var form = new Ext.FormPanel({
			frame: false
			,baseCls: 'x-plain'
			,defaults: {
				xtype: 'fieldset'
				,defaults: {
					xtype: 'fieldset'
					,layout: 'column'
					,checkboxToggle: true
					,listeners: {
						beforecollapse: function(){return false}	
					}
					,onCheckClick: function(e, field){
						this.items.each(function(item){
							item.setValue(field.checked);	
						});
					}
					,defaults: {
						xtype: 'checkbox'
						,hideLabel: true
						,columnWidth: .25
					}
				}
			}
			,items: [
				{
					title: 'Admin'
					,items: [
						{
							title: 'Page'
							,items: [
								{
									name: 'a'
									,boxLabel: 'abc'	
								},{
									name: 'a'
									,boxLabel: 'abc'	
								},{
									name: 'a'
									,boxLabel: 'abc'	
								},{
									name: 'a'
									,boxLabel: 'abc'	
								}
							]
						}
					]
				},{
					title: 'Index'	
				}
			]
			,buttons: [
				{
					text: 'Save'	
				},{
					text: 'Reset',
					handler: function(){form.getForm().reset()}	
				},{
					text: 'Close'
					,handler: function(){o.getWin().hide()}	
				}
			]	
		});
		
		this.form = form;
		
		return this.form;	
	}
	,getItems: function(){
		if(this.items) return this.items;
		
		var form = this.getForm();
		this.items = [form];
		
		return this.items;
	}
	,getWin: function(){
		if(this.win) return this.win;
		
		var items = this.getItems();
		this.win = new App.SubWindow({
			title: 'Role Permission'
			,renderTo: this.tree.ownerCt.body.id
			,plain: true
			,items: items
			,bodyStyle: 'padding:5px'
			
		});
		
		return this.win;
	}
	,showWin: function(bt){
		this.getWin().show(bt.id);
	}
});